//
//  Users.m
//  TabBarDemo
//
//  Created by Bob on 15/3/14.
//  Copyright (c) 2015年 Colin Eberhardt. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Users.h"
@implementation Users
- (Users*)initWithJsonDictionary:(NSDictionary*)dic
{
    self = [super init];
    
    [self updateWithJSonDictionary:dic];
    
    return self;
}
- (void)updateWithJSonDictionary:(NSDictionary*)dic
{
    self.id = [dic objectForKey:@"id"];
    self.user_name = [dic objectForKey:@"user_name"];
    self.face_url = [NSURL URLWithString:((NSString *)[dic objectForKey:@"face_url"])];
    self.face_width = [(NSString*)[dic objectForKey:@"face_width"] integerValue];
    self.face_height = [(NSString*)[dic objectForKey:@"face_height"] integerValue];
    self.gender = [(NSString *)[dic objectForKey:@"gender"] isEqualToString:@"f"]?@"帅哥":@"美女";
    self.astro = [dic objectForKey:@"astro"];
    self.life = [(NSString*)[dic objectForKey:@"life"] integerValue];
    self.level = [dic objectForKey:@"level"];
    self.is_online = (BOOL)[dic objectForKey:@"is_online"];
    self.post_count = [(NSString*)[dic objectForKey:@"post_count"] integerValue];
    self.last_login_time =[self getTimeToShowWithTimestamp:[NSString stringWithFormat:@"(%@)",[dic objectForKey:@"last_login_time"]]];
    self.score = [(NSString*)[dic objectForKey:@"score"] integerValue];
    self.last_login_ip = [dic objectForKey:@"last_login_ip"];
}
+ (Users*)userWithJsonDictionary:(NSDictionary*)dic
{
    Users * user = [[Users alloc]initWithJsonDictionary:dic];
    return user;
}
- (NSString *)stringFromDate:(NSDate *)date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    return destDateString;
}
-(NSDate*) convertDateFromString:(NSString*)uiDate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:@"yyyy年MM月dd日"];
    NSDate *date=[formatter dateFromString:uiDate];
    return date;
}
-(NSString *)getTimeToShowWithTimestamp:(NSString *)timestamp
{
    int start = 0;//“（”的位置
    int end = 0;//“）”的位置
    for (int i = 0; i < timestamp.length; i++) {
        if ([timestamp characterAtIndex:i] == '(') {
            start = i;
        }
        if ([timestamp characterAtIndex:i] == ')') {
            end = i;
        }
    }
    NSString *publishString = [timestamp substringToIndex:end];
    publishString = [publishString substringFromIndex:start + 1];
    
    double publishLong = [publishString doubleValue];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [formatter setDateFormat:@"yyyy.MM.dd HH:mm:ss"];
    
    NSDate *publishDate = [NSDate dateWithTimeIntervalSince1970:publishLong];
    
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:date];
    publishDate = [publishDate  dateByAddingTimeInterval: interval];
    
    publishString = [formatter stringFromDate:publishDate];
    
    return publishString;
}

@end
